   /*


   */

struct Index
{
  short  Folder;
  short  Table;
  short  Value;
};

#include<io.h>
#include<fstream.h>
#include<math.h>
#include<string.h>
#include<alloc.h>
int ans;
/*
class FileToBinary
{
public:

  createBinaryFile(char fname[])
  {
	ifstream fin(fname[],ios::binary);

	  if(!fin)
	  {
		 cout<<"File Not Found %s"<<fname;
		 exit(0);
	  }

	  while(1)
	  {

		 fin.read((char)ch,sizeof(char));

		 if(ch==EOF)
		   break;
		 writeBits(ch);


	  }
	 fin.close();
   //this method will create temp.neo file which is in binary no stored in text
  }

 writeBits(int ch)
{
   int i,k,mask;

	ofstream fout("c:\bits.neo",ios::nocreate);

	  if(!fout)
		 ofstream fout("c:\bits.neo");
	  else
		   fout("c:\bits.neo",ios::app);

  for(i=7;i>=0;i--)
   {
	 mask=1<<i;
	 k=ch&mask;
	 if(k==0)
	  fout<<"0";
	 else
	  fout<<"1";
   }
   fout.close();
 }
}

*/
class BinaryToCompression
{
 private:
		struct Index *index;
	   void	writeBits(int,int );
 public:
	   void getValue(unsigned char);
	   void putValue();
	   void binaryToNumber(int ,int);

 //using table 8 bits are converted 2 6 bits
};

int main(int argv,char *argc[])
 {
	BinaryToCompression coutcomp;
	unsigned char no;
	char fname[30];
	  if(argv==1)
	  {
		cout<<"Enter File Name";
		cin>>fname;
		strcpy(argc[1],fname);
	  }


	ifstream fin(argc[1]);
	  if(!fin)
	   {
		cout<<"Could not open %s"<<argc[1];
		return 0;
	   }

   while(fin.get(no))
   {
	   coutcomp.getValue(no);
	   coutcomp.putValue();
	}

   fin.close();

   ifstream fin1("c:/bits.neo");
   strcpy(fname,argc[1]);
   strcat(fname,".neo\0");
   ofstream fout1("c:/vish.neo");
	  if(!fin1)
	   {
		cout<<"Could not open bits.neo";
		return 0;
	   }
   char bit=1;

	while(bit!=EOF)
	 {
	 ans=0;
	  for(int i=7;i>=0;i--)
	   {
	   fin1.get(bit);
		if(bit==EOF)
		  break;
		coutcomp.binaryToNumber(no,i);
	   }
	   fout1.put(ans);
	 }
   unlink("c:/bits.neo");
	//close all file and delete all temp file which was created during compression
  return 0;
 }


void BinaryToCompression::getValue(unsigned char no)
	{
	  index=NULL;
	  if(no<128)
	  {
		index = (struct Index *)malloc(sizeof(struct Index ));
		  index->Folder=0;
		  if(no>=0 && no<=7)    //1
		   {
		   index->Table=0;
		   index->Value=no;
		   break;
		   }
		   else
		   if(no>=8&&no<=15)     //2
		   {
		   index->Table=0;
		   index->Value=no-8;
		   break;
		   }
		   else
		   if(no>=16&&no<=23)       //3
		   {
		   index->Table=0;
		   index->Value=no-16;
		   break;
		   }
		   else
		   if(no>=24&&no<=31)        //4
		   {
		   index->Table=0;
		   index->Value=no-24;
		   break;
		   }
		   if(no>=32&&no<=39)           //5
		   {
		   index->Table=1;
		   index->Value=no-32;
		   break;
		   }
		   else
		   if(no>=40&&no<=47)     //6
		   {
		   index->Table=1;
		   index->Value=no-40;
		   break;
		   }
		   else
		   if(no>=48&&no<=55)        //7
		   {
		   index->Table=1;
		   index->Value=no-48;
		   break;
		   }
		   else
		   if(no>=56&&no<=63)           //8
		   {
		   index->Table=1;
		   index->Value=no-56;
		   break;
		   }
			  if(no>=64&&no<=71)           //9
		   {
		   index->Table=2;
		   index->Value=no-64;
		   break;
		   }
		   else
		   if(no>=72&&no<=79)                 //10
		   {
		   index->Table=2;
		   index->Value=no-72;
		   break;
		   }
		   else
		   if(no>=80&&no<=87)           //11
		   {
		   index->Table=2;
		   index->Value=no-80;
		   break;
		   }
		   else
		   if(no>=88&&no<=95)               //12
		   {
		   index->Table=2;
		   index->Value=no-88;
		   break;
		   }
		   if(no>=96&&no<=103)                  //13
		   {
		   index->Table=3;
		   index->Value=no-96;
		   break;
		   }
		   else
		   if(no>=104&&no<=111)       //14
		   {
		   index->Table=3;
		   index->Value=no-104;
		   break;
		   }
		   else
		   if(no>=112&&no<=119)         //15
		   {
		   index->Table=3;
		   index->Value=no-112;
		   break;
		   }
		   else
		   if(no>=120&&no<=127)    //16
		   {
		   index->Table=3;
		   index->Value=no-120;
		   break;
		   }
	   }
	  else
	  if(no>=128)
	  {

		index = (struct Index *)malloc(sizeof(struct Index ));
		  index->Folder=1;
		  if(no>=128&&no<=135)
		   {
		   index->Table=0;
		   index->Value=no-128;
		   break;
		   }
		   else
		   if(no>=136&&no<=143)
		   {
		   index->Table=0;
		   index->Value=no-136;
		   break;
		   }
		   else
		   if(no>=144&&no<=151)
		   {
		   index->Table=0;
		   index->Value=no-144;
		   break;
		   }
		   else
		   if(no>=152&&no<=159)
		   {
		   index->Table=0;
		   index->Value=no-152;
		   break;
		   }
		   if(no>=160&&no<=167)
		   {
		   index->Table=1;
		   index->Value=no-160;
		   break;
		   }
		   else
		   if(no>=168&&no<=175)
		   {
		   index->Table=1;
		   index->Value=no-168;
		   break;
		   }
		   else
		   if(no>=176&&no<=183)
		   {
		   index->Table=1;
		   index->Value=no-176;
		   break;
		   }
		   else
		   if(no>=184&&no<=191)
		   {
		   index->Table=1;
		   index->Value=no-184;
		   break;
		   }
		   if(no>=192&&no<=199)
		   {
		   index->Table=2;
		   index->Value=no-192;
		   break;
		   }
		   else
		   if(no>=200&&no<=207)
		   {
		   index->Table=2;
		   index->Value=no-200;
		   break;
		   }
		   else
		   if(no>=208&&no<=215)
		   {
		   index->Table=2;
		   index->Value=no-208;
		   break;
		   }
		   else
		   if(no>=216&&no<=223)
		   {
		   index->Table=2;
		   index->Value=no-216;
		   break;
		   }
		   if(no>=224&&no<=231)
		   {
		   index->Table=3;
		   index->Value=no-224;
		   break;
		   }
		   else
		   if(no>=232&&no<=239)
		   {
		   index->Table=3;
		   index->Value=no-232;
		   break;
		   }
		   else
		   if(no>=240&&no<=247)
		   {
		   index->Table=3;
		   index->Value=no-240;
		   break;
		   }
		   else
		   if(no>=248&&no<=255)
		   {
		   index->Table=3;
		   index->Value=no-248;
		   break;
		   }
	  }
	  else
	  {
		cout<<"error in getvalue() during number";
	  }

	}
	//End Of Method getValue
//---------------------------------------------------------------------------

void BinaryToCompression::writeBits(int ch,int limit)
  {
   int i,k,mask;

	//ofstream fout("c:/bits.neo");
	 ofstream fout("c:/bits.neo",ios::app);
	/*  if(fout)
		{
		 fout.close();
		 ofstream fout("c:/bits.neo",ios::app);
		}
	  */
  for(i=limit;i>=0;i--)
   {
	 mask=1<<i;
	 k=ch&mask;
	 if(k==0)
	  fout.put('0');
	 else
	  fout.put('1');
	}
	fout.close();
   }

void BinaryToCompression::putValue()
	{
	 writeBits(index->Folder,0);
	 writeBits(index->Table,1);
	 writeBits(index->Value,2);
	}


 void  BinaryToCompression::binaryToNumber(int no,int i)
 {
	if(no=='1')
	 ans=ans+pow(2,i);
   else
	 ans=ans+0;
 }


